<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>
<
</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9272268.html">Redis（三）Redis基本命令操作与API</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2 class="prettyprint prettyprinted"><span class="pln">一Redis 连接</span></h2>
<p>Redis 连接命令主要是用于连接 redis 服务。</p>
<p><strong>实例</strong></p>
<p>以下实例演示了客户端如何通过密码验证连接到 redis 服务，并检测服务是否在运行：</p>
<pre class="prettyprint">redis 127.0.0.1:6379&gt; AUTH "password"
OK
redis 127.0.0.1:6379&gt; PING
PONG</pre>
<p>下表列出了 redis 连接的基本命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/connection-auth.html">AUTH password</a>&nbsp;<br />验证密码是否正确</td>




</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/connection-echo.html">ECHO message</a>&nbsp;<br />打印字符串</td>




</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/connection-ping.html">PING</a>&nbsp;<br />查看服务是否运行</td>




</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/connection-quit.html">QUIT</a>&nbsp;<br />关闭当前连接</td>




</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/connection-select.html">SELECT index</a>&nbsp;<br />切换到指定的数据库<br /><br /></td>




</tr>




</tbody>



</table>
<h2>二Redis 键(key)</h2>
<p>Redis key命令用于管理 redis 的键。</p>
<p><strong>语法</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> COMMAND KEY_NAME</span></span></span></span></span></span></span></span></pre>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SET runoobkey redis
OK
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> DEL runoobkey
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>在以上实例中&nbsp;<strong>DEL</strong>&nbsp;是一个命令，&nbsp;<strong>runoobkey</strong>&nbsp;是一个键。 如果键被删除成功，命令执行后输出&nbsp;<strong>(integer) 1</strong>，否则将输出&nbsp;<strong>(integer) 0</strong></p>
<p>下表给出了与 Redis 键相关的基本命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/keys-del.html">DEL key</a><br />该命令用于在 key 存在时删除 key。</td>















</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/keys-dump.html">DUMP key</a>&nbsp;<br />序列化给定 key ，并返回被序列化的值。</td>















</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/keys-exists.html">EXISTS key</a>&nbsp;<br />检查给定 key 是否存在。</td>















</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/keys-expire.html">EXPIRE key</a>&nbsp;seconds<br />为给定 key 设置过期时间。</td>















</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/keys-expireat.html">EXPIREAT key timestamp</a>&nbsp;<br />EXPIREAT 的作用和 EXPIRE 类似，都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。</td>















</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/keys-pexpire.html">PEXPIRE key milliseconds</a>&nbsp;<br />设置 key 的过期时间以毫秒计。</td>















</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/keys-pexpireat.html">PEXPIREAT key milliseconds-timestamp</a>&nbsp;<br />设置 key 过期时间的时间戳(unix timestamp) 以毫秒计</td>















</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/keys-keys.html">KEYS pattern</a>&nbsp;<br />查找所有符合给定模式( pattern)的 key 。</td>















</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/keys-move.html">MOVE key db</a>&nbsp;<br />将当前数据库的 key 移动到给定的数据库 db 当中。</td>















</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/keys-persist.html">PERSIST key</a>&nbsp;<br />移除 key 的过期时间，key 将持久保持。</td>















</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/keys-pttl.html">PTTL key</a>&nbsp;<br />以毫秒为单位返回 key 的剩余的过期时间。</td>















</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/keys-ttl.html">TTL key</a>&nbsp;<br />以秒为单位，返回给定 key 的剩余生存时间(TTL, time to live)。</td>















</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/keys-randomkey.html">RANDOMKEY</a>&nbsp;<br />从当前数据库中随机返回一个 key 。</td>















</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/keys-rename.html">RENAME key newkey</a>&nbsp;<br />修改 key 的名称</td>















</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/keys-renamenx.html">RENAMENX key newkey</a>&nbsp;<br />仅当 newkey 不存在时，将 key 改名为 newkey 。</td>















</tr>
<tr>
<td>16</td>
<td><a href="http://www.runoob.com/redis/keys-type.html">TYPE key</a>&nbsp;<br />返回 key 所储存的值的类型。</td>















</tr>















</tbody>














</table>
<h2>三Redis 数据类型</h2>
<div class="article-body">
<div id="content" class="article-intro">
<p>Redis支持五种数据类型：string（字符串），hash（哈希），list（列表），set（集合）及zset(sorted set：有序集合)。各个数据类型应用场景如下</p>
<table class="reference">
<thead>
<tr><th>类型</th><th>简介</th><th>特性</th><th>场景</th></tr>







</thead>
<tbody>
<tr>
<td>String(字符串)</td>
<td>二进制安全</td>
<td>可以包含任何数据,比如jpg图片或者序列化的对象,一个键最大能存储512M</td>
<td>&nbsp;</td>







</tr>
<tr>
<td>Hash(字典)</td>
<td>键值对集合,即编程语言中的Map类型</td>
<td>适合存储对象,并且可以像数据库中update一个属性一样只修改某一项属性值(Memcached中需要取出整个字符串反序列化成对象修改完再序列化存回去)</td>
<td>存储、读取、修改用户属性</td>







</tr>
<tr>
<td>List(列表)</td>
<td>链表(双向链表)</td>
<td>增删快,提供了操作某一段元素的API</td>
<td>1,最新消息排行等功能(比如朋友圈的时间线) 2,消息队列</td>







</tr>
<tr>
<td>Set(集合)</td>
<td>哈希表实现,元素不重复</td>
<td>1,添加、删除,查找的复杂度都是O(1) 2,为集合提供了求交集、并集、差集等操作</td>
<td>1,共同好友 2,利用唯一性,统计访问网站的所有独立ip 3,好用推荐时,根据tag求交集,大于某个阈值就可以推荐</td>







</tr>
<tr>
<td>Sorted Set(有序集合)</td>
<td>将Set中的元素增加一个权重参数score,元素按score有序排列</td>
<td>数据插入集合时,已经进行天然排序</td>
<td>1,排行榜 2,带权重的消息队列<br /><br /></td>































</tr>































</tbody>






























</table>
<h3>String（字符串）</h3>
<p>string是redis最基本的类型，你可以理解成与Memcached一模一样的类型，一个key对应一个value，<span style="color: #ff0000;">一个键最大能存储512MB</span>。string类型是二进制安全的，意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象 。</p>
<p><strong>语法</strong></p>
<p class="prettyprint prettyprinted"><span class="pln">redis&nbsp;<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln">&nbsp;COMMAND KEY_NAME</span></span></span></span></span></span></span></span></p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SET runoobkey redis
OK
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> GET runoobkey
<span class="str">"redis"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>下表列出了常用的 redis 字符串命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/strings-set.html">SET key value</a>&nbsp;<br />设置指定 key 的值</td>













</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/strings-get.html">GET key</a>&nbsp;<br />获取指定 key 的值。</td>













</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/strings-getrange.html">GETRANGE key start end</a>&nbsp;<br />返回 key 中字符串值的子字符</td>













</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/strings-getset.html">GETSET key value</a><br />将给定 key 的值设为 value ，并返回 key 的旧值(old value)。</td>













</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/strings-getbit.html">GETBIT key offset</a><br />对 key 所储存的字符串值，获取指定偏移量上的位(bit)。</td>













</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/strings-mget.html">MGET key1 [key2..]</a><br />获取所有(一个或多个)给定 key 的值。</td>













</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/strings-setbit.html">SETBIT key offset value</a><br />对 key 所储存的字符串值，设置或清除指定偏移量上的位(bit)。</td>













</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/strings-setex.html">SETEX key seconds value</a><br />将值 value 关联到 key ，并将 key 的过期时间设为 seconds (以秒为单位)。</td>













</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/strings-setnx.html">SETNX key value</a><br />只有在 key 不存在时设置 key 的值。</td>













</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/strings-setrange.html">SETRANGE key offset value</a><br />用 value 参数覆写给定 key 所储存的字符串值，从偏移量 offset 开始。</td>













</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/strings-strlen.html">STRLEN key</a><br />返回 key 所储存的字符串值的长度。</td>













</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/strings-mset.html">MSET key value [key value ...]</a><br />同时设置一个或多个 key-value 对。</td>













</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/strings-msetnx.html">MSETNX key value [key value ...]</a>&nbsp;<br />同时设置一个或多个 key-value 对，当且仅当所有给定 key 都不存在。</td>













</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/strings-psetex.html">PSETEX key milliseconds value</a><br />这个命令和 SETEX 命令相似，但它以毫秒为单位设置 key 的生存时间，而不是像 SETEX 命令那样，以秒为单位。</td>













</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/strings-incr.html">INCR key</a><br />将 key 中储存的数字值增一。</td>













</tr>
<tr>
<td>16</td>
<td><a href="http://www.runoob.com/redis/strings-incrby.html">INCRBY key increment</a><br />将 key 所储存的值加上给定的增量值（increment） 。</td>













</tr>
<tr>
<td>17</td>
<td><a href="http://www.runoob.com/redis/strings-incrbyfloat.html">INCRBYFLOAT key increment</a><br />将 key 所储存的值加上给定的浮点增量值（increment） 。</td>













</tr>
<tr>
<td>18</td>
<td><a href="http://www.runoob.com/redis/strings-decr.html">DECR key</a><br />将 key 中储存的数字值减一。</td>













</tr>
<tr>
<td>19</td>
<td><a href="http://www.runoob.com/redis/strings-decrby.html">DECRBY key decrement</a><br />key 所储存的值减去给定的减量值（decrement） 。</td>













</tr>
<tr>
<td>20</td>
<td><a href="http://www.runoob.com/redis/strings-append.html">APPEND key value</a><br />如果 key 已经存在并且是一个字符串， APPEND 命令将指定的 value 追加到该 key 原来值（value）的末尾。</td>













</tr>













</tbody>












</table>
<h3>Hash（哈希）</h3>
<p>hash 是一个键值(key=&gt;value)对集合，是一个 string 类型的 field 和 value 的映射表，特别适合用于存储对象。每个 hash 可以存储 2<sup>32</sup>&nbsp;-1 键值对（40多亿）。</p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln">  HMSET runoobkey name <span class="str">"redis tutorial"<span class="pln"> description <span class="str">"redis basic commands for caching"<span class="pln"> likes <span class="lit">20<span class="pln"> visitors <span class="lit">23000<span class="pln">
OK
<span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln">  HGETALL runoobkey
<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"name"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"redis tutorial"<span class="pln">
<span class="lit">3<span class="pun">)<span class="pln"> <span class="str">"description"<span class="pln">
<span class="lit">4<span class="pun">)<span class="pln"> <span class="str">"redis basic commands for caching"<span class="pln">
<span class="lit">5<span class="pun">)<span class="pln"> <span class="str">"likes"<span class="pln">
<span class="lit">6<span class="pun">)<span class="pln"> <span class="str">"20"<span class="pln">
<span class="lit">7<span class="pun">)<span class="pln"> <span class="str">"visitors"<span class="pln">
<span class="lit">8<span class="pun">)<span class="pln"> <span class="str">"23000"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>在以上实例中，我们设置了 redis 的一些描述信息(name, description, likes, visitors) 到哈希表的&nbsp;<strong>runoobkey</strong>&nbsp;中。</p>
<p>下表列出了 redis hash 基本的相关命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/hashes-hdel.html">HDEL key field1 [field2]</a>&nbsp;<br />删除一个或多个哈希表字段</td>












</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/hashes-hexists.html">HEXISTS key field</a>&nbsp;<br />查看哈希表 key 中，指定的字段是否存在。</td>












</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/hashes-hget.html">HGET key field</a>&nbsp;<br />获取存储在哈希表中指定字段的值。</td>












</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/hashes-hgetall.html">HGETALL key</a>&nbsp;<br />获取在哈希表中指定 key 的所有字段和值</td>












</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/hashes-hincrby.html">HINCRBY key field increment</a>&nbsp;<br />为哈希表 key 中的指定字段的整数值加上增量 increment 。</td>












</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/hashes-hincrbyfloat.html">HINCRBYFLOAT key field increment</a>&nbsp;<br />为哈希表 key 中的指定字段的浮点数值加上增量 increment 。</td>












</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/hashes-hkeys.html">HKEYS key</a>&nbsp;<br />获取所有哈希表中的字段</td>












</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/hashes-hlen.html">HLEN key</a>&nbsp;<br />获取哈希表中字段的数量</td>












</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/hashes-hmget.html">HMGET key field1 [field2]</a>&nbsp;<br />获取所有给定字段的值</td>












</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/hashes-hmset.html">HMSET key field1 value1 [field2 value2 ]</a>&nbsp;<br />同时将多个 field-value (域-值)对设置到哈希表 key 中。</td>












</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/hashes-hset.html">HSET key field value</a>&nbsp;<br />将哈希表 key 中的字段 field 的值设为 value 。</td>












</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/hashes-hsetnx.html">HSETNX key field value</a>&nbsp;<br />只有在字段 field 不存在时，设置哈希表字段的值。</td>












</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/hashes-hvals.html">HVALS key</a>&nbsp;<br />获取哈希表中所有值</td>












</tr>
<tr>
<td>14</td>
<td>HSCAN key cursor [MATCH pattern] [COUNT count]&nbsp;<br />迭代哈希表中的键值对。</td>












</tr>












</tbody>












</table>
<h3>List（列表）</h3>
<p>列表是简单的字符串列表，按照插入顺序排序。你可以添加一个元素到列表的左边或者右边。列表最多可存储 2<sup>32</sup>&nbsp;- 1 元素 (4294967295, 每个列表可存储40多亿)。</p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> LPUSH runoobkey redis
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> LPUSH runoobkey mongodb
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">2<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> LPUSH runoobkey mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">3<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> LRANGE runoobkey <span class="lit">0<span class="pln"> <span class="lit">10<span class="pln">

<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"mysql"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"mongodb"<span class="pln">
<span class="lit">3<span class="pun">)<span class="pln"> <span class="str">"redis"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>在以上实例中我们使用了&nbsp;<strong>LPUSH</strong>&nbsp;将三个值插入了名为&nbsp;<strong>runoobkey</strong>&nbsp;的列表当中。</p>
<p>下表列出了列表相关的基本命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/lists-blpop.html" target="_blank">BLPOP key1 [key2 ] timeout</a>&nbsp;<br />移出并获取列表的第一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>






















</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/lists-brpop.html" target="_blank">BRPOP key1 [key2 ] timeout</a>&nbsp;<br />移出并获取列表的最后一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>






















</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/lists-brpoplpush.html" target="_blank">BRPOPLPUSH source destination timeout</a>&nbsp;<br />从列表中弹出一个值，将弹出的元素插入到另外一个列表中并返回它； 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>






















</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/lists-lindex.html" target="_blank">LINDEX key index</a>&nbsp;<br />通过索引获取列表中的元素</td>






















</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/lists-linsert.html" target="_blank">LINSERT key BEFORE|AFTER pivot value</a>&nbsp;<br />在列表的元素前或者后插入元素</td>






















</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/lists-llen.html" target="_blank">LLEN key</a>&nbsp;<br />获取列表长度</td>






















</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/lists-lpop.html" target="_blank">LPOP key</a>&nbsp;<br />移出并获取列表的第一个元素</td>






















</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/lists-lpush.html" target="_blank">LPUSH key value1 [value2]</a>&nbsp;<br />将一个或多个值插入到列表头部</td>






















</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/lists-lpushx.html" target="_blank">LPUSHX key value</a>&nbsp;<br />将一个值插入到已存在的列表头部</td>






















</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/lists-lrange.html" target="_blank">LRANGE key start stop</a>&nbsp;<br />获取列表指定范围内的元素</td>






















</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/lists-lrem.html" target="_blank">LREM key count value</a>&nbsp;<br />移除列表元素</td>






















</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/lists-lset.html" target="_blank">LSET key index value</a>&nbsp;<br />通过索引设置列表元素的值</td>






















</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/lists-ltrim.html" target="_blank">LTRIM key start stop</a>&nbsp;<br />对一个列表进行修剪(trim)，就是说，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除。</td>






















</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/lists-rpop.html" target="_blank">RPOP key</a>&nbsp;<br />移除并获取列表最后一个元素</td>






















</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/lists-rpoplpush.html" target="_blank">RPOPLPUSH source destination</a>&nbsp;<br />移除列表的最后一个元素，并将该元素添加到另一个列表并返回</td>






















</tr>
<tr>
<td>16</td>
<td><a href="http://www.runoob.com/redis/lists-rpush.html" target="_blank">RPUSH key value1 [value2]</a>&nbsp;<br />在列表中添加一个或多个值</td>






















</tr>
<tr>
<td>17</td>
<td><a href="http://www.runoob.com/redis/lists-rpushx.html" target="_blank">RPUSHX key value</a>&nbsp;<br />为已存在的列表添加值</td>






















</tr>






















</tbody>





















</table>
<h3>Set（集合）</h3>
<p>Set是string类型的无序集合。集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。集合中最大的成员数为 2<sup>32</sup>&nbsp;- 1(4294967295, 每个集合可存储40多亿个成员)。</p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SADD runoobkey redis
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SADD runoobkey mongodb
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SADD runoobkey mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SADD runoobkey mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">0<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> SMEMBERS runoobkey

<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"mysql"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"mongodb"<span class="pln">
<span class="lit">3<span class="pun">)<span class="pln"> <span class="str">"redis"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>在以上实例中我们通过&nbsp;<strong>SADD</strong>&nbsp;命令向名为&nbsp;<strong>runoobkey</strong>&nbsp;的集合插入的三个元素。</p>
<p>下表列出了 Redis 集合基本命令：</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/sets-sadd.html">SADD key member1 [member2]</a>&nbsp;<br />向集合添加一个或多个成员</td>








</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/sets-scard.html">SCARD key</a>&nbsp;<br />获取集合的成员数</td>








</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/sets-sdiff.html">SDIFF key1 [key2]</a>&nbsp;<br />返回给定所有集合的差集</td>








</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/sets-sdiffstore.html">SDIFFSTORE destination key1 [key2]</a>&nbsp;<br />返回给定所有集合的差集并存储在 destination 中</td>








</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/sets-sinter.html">SINTER key1 [key2]</a>&nbsp;<br />返回给定所有集合的交集</td>








</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/sets-sinterstore.html">SINTERSTORE destination key1 [key2]</a>&nbsp;<br />返回给定所有集合的交集并存储在 destination 中</td>








</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/sets-sismember.html">SISMEMBER key member</a>&nbsp;<br />判断 member 元素是否是集合 key 的成员</td>








</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/sets-smembers.html">SMEMBERS key</a>&nbsp;<br />返回集合中的所有成员</td>








</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/sets-smove.html">SMOVE source destination member</a>&nbsp;<br />将 member 元素从 source 集合移动到 destination 集合</td>








</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/sets-spop.html">SPOP key</a>&nbsp;<br />移除并返回集合中的一个随机元素</td>








</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/sets-srandmember.html">SRANDMEMBER key [count]</a>&nbsp;<br />返回集合中一个或多个随机数</td>








</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/sets-srem.html">SREM key member1 [member2]</a>&nbsp;<br />移除集合中一个或多个成员</td>








</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/sets-sunion.html">SUNION key1 [key2]</a>&nbsp;<br />返回所有给定集合的并集</td>








</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/sets-sunionstore.html">SUNIONSTORE destination key1 [key2]</a>&nbsp;<br />所有给定集合的并集存储在 destination 集合中</td>








</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/sets-sscan.html">SSCAN key cursor [MATCH pattern] [COUNT count]</a>&nbsp;<br />迭代集合中的元素</td>








</tr>








</tbody>







</table>
<h3>zset(sorted set:有序集合)</h3>







zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
<p>zset的成员是唯一的,但分数(score)却可以重复。集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。 集合中最大的成员数为 2<sup>32 - 1</sup>&nbsp;(4294967295, 每个集合可存储40多亿个成员)。</p>
<p><strong>实例</strong></p>
<pre class="prettyprint prettyprinted"><span class="pln">redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZADD runoobkey <span class="lit">1<span class="pln"> redis
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZADD runoobkey <span class="lit">2<span class="pln"> mongodb
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZADD runoobkey <span class="lit">3<span class="pln"> mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">1<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZADD runoobkey <span class="lit">3<span class="pln"> mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">0<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZADD runoobkey <span class="lit">4<span class="pln"> mysql
<span class="pun">(<span class="pln">integer<span class="pun">)<span class="pln"> <span class="lit">0<span class="pln">
redis <span class="lit">127.0<span class="pun">.<span class="lit">0.1<span class="pun">:<span class="lit">6379<span class="pun">&gt;<span class="pln"> ZRANGE runoobkey <span class="lit">0<span class="pln"> <span class="lit">10<span class="pln"> WITHSCORES

<span class="lit">1<span class="pun">)<span class="pln"> <span class="str">"redis"<span class="pln">
<span class="lit">2<span class="pun">)<span class="pln"> <span class="str">"1"<span class="pln">
<span class="lit">3<span class="pun">)<span class="pln"> <span class="str">"mongodb"<span class="pln">
<span class="lit">4<span class="pun">)<span class="pln"> <span class="str">"2"<span class="pln">
<span class="lit">5<span class="pun">)<span class="pln"> <span class="str">"mysql"<span class="pln">
<span class="lit">6<span class="pun">)<span class="pln"> <span class="str">"4"</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p>在以上实例中我们通过命令&nbsp;<strong>ZADD</strong>&nbsp;向 redis 的有序集合中添加了三个值并关联上分数。</p>
<p>下表列出了 redis 有序集合的基本命令:</p>
<table class="reference">
<tbody>
<tr><th>序号</th><th>命令及描述</th></tr>
<tr>
<td>1</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zadd.html">ZADD key score1 member1 [score2 member2]</a>&nbsp;<br />向有序集合添加一个或多个成员，或者更新已存在成员的分数</td>




















</tr>
<tr>
<td>2</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zcard.html">ZCARD key</a>&nbsp;<br />获取有序集合的成员数</td>




















</tr>
<tr>
<td>3</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zcount.html">ZCOUNT key min max</a>&nbsp;<br />计算在有序集合中指定区间分数的成员数</td>




















</tr>
<tr>
<td>4</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zincrby.html">ZINCRBY key increment member</a>&nbsp;<br />有序集合中对指定成员的分数加上增量 increment</td>




















</tr>
<tr>
<td>5</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zinterstore.html">ZINTERSTORE destination numkeys key [key ...]</a>&nbsp;<br />计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中</td>




















</tr>
<tr>
<td>6</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zlexcount.html">ZLEXCOUNT key min max</a>&nbsp;<br />在有序集合中计算指定字典区间内成员数量</td>




















</tr>
<tr>
<td>7</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrange.html">ZRANGE key start stop [WITHSCORES]</a>&nbsp;<br />通过索引区间返回有序集合成指定区间内的成员</td>




















</tr>
<tr>
<td>8</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrangebylex.html">ZRANGEBYLEX key min max [LIMIT offset count]</a>&nbsp;<br />通过字典区间返回有序集合的成员</td>




















</tr>
<tr>
<td>9</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrangebyscore.html">ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]</a>&nbsp;<br />通过分数返回有序集合指定区间内的成员</td>




















</tr>
<tr>
<td>10</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrank.html">ZRANK key member</a>&nbsp;<br />返回有序集合中指定成员的索引</td>




















</tr>
<tr>
<td>11</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrem.html">ZREM key member [member ...]</a>&nbsp;<br />移除有序集合中的一个或多个成员</td>




















</tr>
<tr>
<td>12</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zremrangebylex.html">ZREMRANGEBYLEX key min max</a>&nbsp;<br />移除有序集合中给定的字典区间的所有成员</td>




















</tr>
<tr>
<td>13</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zremrangebyrank.html">ZREMRANGEBYRANK key start stop</a>&nbsp;<br />移除有序集合中给定的排名区间的所有成员</td>




















</tr>
<tr>
<td>14</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zremrangebyscore.html">ZREMRANGEBYSCORE key min max</a>&nbsp;<br />移除有序集合中给定的分数区间的所有成员</td>




















</tr>
<tr>
<td>15</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrevrange.html">ZREVRANGE key start stop [WITHSCORES]</a>&nbsp;<br />返回有序集中指定区间内的成员，通过索引，分数从高到底</td>




















</tr>
<tr>
<td>16</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrevrangebyscore.html">ZREVRANGEBYSCORE key max min [WITHSCORES]</a>&nbsp;<br />返回有序集中指定分数区间内的成员，分数从高到低排序</td>




















</tr>
<tr>
<td>17</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zrevrank.html">ZREVRANK key member</a>&nbsp;<br />返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序</td>




















</tr>
<tr>
<td>18</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zscore.html">ZSCORE key member</a>&nbsp;<br />返回有序集中，成员的分数值</td>




















</tr>
<tr>
<td>19</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zunionstore.html">ZUNIONSTORE destination numkeys key [key ...]</a>&nbsp;<br />计算给定的一个或多个有序集的并集，并存储在新的 key 中</td>




















</tr>
<tr>
<td>20</td>
<td><a href="http://www.runoob.com/redis/sorted-sets-zscan.html">ZSCAN key cursor [MATCH pattern] [COUNT count]</a>&nbsp;<br />迭代有序集合中的元素（包括元素成员和元素分值）</td>




















</tr>




















</tbody>



















</table>
<h2>四 Redis的API操作</h2>






</div>






</div>
<h3 id="jedis客户端"><strong>单机版</strong></h3>
<p>Jedis 是比较主流的 Redis Java 客户端。<br />第一步：导入Jedis需要的jar</p>
<div class="sourceCode">
<div class="cnblogs_code">
<pre>&lt;!-- Redis客户端 --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;redis.clients&lt;/groupId&gt;
    &lt;artifactId&gt;jedis&lt;/artifactId&gt;
    &lt;jedis.version&gt;2.7.2&lt;/jedis.version&gt;
&lt;/dependency&gt;</pre>
</div>
<p>第二步：单元测试类<br />Jedis 的语法和 Redis 几乎一样，如果学好了Redis，Jedis也就没问题了，可谓是买一送一。建议使用连接池的方式。</p>
<div class="cnblogs_code">
<pre><span>package com.itdragon.redis;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class<span> TestJedisOperate {
    
    private final static String HOST = "112.74.83.71"<span>;
    private final static int PORT = 6379<span>;

    /**
     * jedis 的语法和 redis 的语法几乎一致，比较常用的有Hash，String，List
     */<span>
    @Test
    public void<span> jedisSignle() {
        Jedis jedis = new<span> Jedis(HOST, PORT);
        jedis.set("account", "itdragon"<span>);
        System.out.println("set , get 操作 : " + jedis.get("account"<span>));
        jedis.mset("account:01", "itdragon01", "account:02", "itdragon02"<span>);
        System.out.println("mset , mget 操作 : " + jedis.mget("account:01", "account:02"<span>));
        jedis.hset("user", "name", "ITDragon"<span>);
        System.out.println("hset , hget 操作 : " + jedis.hget("user", "name"<span>));
        Map&lt;String, String&gt; userMap = new HashMap&lt;&gt;<span>();
        userMap.put("password", "123456"<span>);
        userMap.put("position", "Java"<span>);
        jedis.hmset("user"<span>, userMap);
        System.out.println("hmset , hmget 操作 : " + jedis.hmget("user", "name", "password", "position"<span>));
        if (0 == jedis.llen("userList"<span>)) {
            jedis.lpush("userList", "1", "2", "3"<span>);
        }
        System.out.println("List 类型 lpush , lrange 操作 : " + jedis.lrange("userList", 0, -1<span>));
        jedis.sadd("userSet", "1", "2", "2"<span>);
        System.out.println("Set 类型 sadd , smembers 操作 : " + jedis.smembers("userSet"<span>));
        Map&lt;String, Double&gt; scoreMembers = new HashMap&lt;&gt;<span>();
        scoreMembers.put("A", 65.0<span>);
        scoreMembers.put("C", 67.0<span>);
        scoreMembers.put("B", 66.0<span>);
        jedis.zadd("userScore"<span>, scoreMembers);
        System.out.println("Set 类型 zadd , zrange 操作 : " + jedis.zrange("userScore", 0, -1<span>));
        jedis.close();
    }
    
    @Test
    public void<span> testJedisPool() {
        JedisPool pool = new<span> JedisPool(HOST, PORT);
        Jedis jedis =<span> pool.getResource();
        System.out.println("通过连接池获取 key 为 account 的值 : " + jedis.get("account"<span>));
        jedis.close();
        pool.close();
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<h4 id="spring-整合-redis">Spring 整合 Redis</h4>
<p>创建用于整合redis的文件 applicationContext-jedis.xml</p>
<div class="cnblogs_code">
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"<span>
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"<span>
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<span>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<span>
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"&gt;
    &lt;!-- 加载配置文件 --&gt;
    &lt;context:property-placeholder location="classpath:resource/*.properties" /&gt;
    &lt;!-- 连接池配置 (可以用 redis 默认配置，效果可能会更好)--&gt;
    &lt;bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"&gt;
        &lt;!-- 最大连接数 --&gt;
        &lt;property name="maxTotal" value="30" /&gt;
        &lt;!-- 最大空闲连接数 --&gt;
        &lt;property name="maxIdle" value="10" /&gt;
        &lt;!-- 每次释放连接的最大数目 --&gt;
        &lt;property name="numTestsPerEvictionRun" value="1024" /&gt;
        &lt;!-- 释放连接的扫描间隔（毫秒） --&gt;
        &lt;property name="timeBetweenEvictionRunsMillis" value="30000" /&gt;
        &lt;!-- 连接最小空闲时间 --&gt;
        &lt;property name="minEvictableIdleTimeMillis" value="1800000" /&gt;
        &lt;!-- 连接空闲多久后释放, 当空闲时间&gt;该值 且 空闲连接&gt;最大空闲连接数 时直接释放 --&gt;
        &lt;property name="softMinEvictableIdleTimeMillis" value="10000" /&gt;
        &lt;!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --&gt;
        &lt;property name="maxWaitMillis" value="1500" /&gt;
        &lt;!-- 在获取连接的时候检查有效性, 默认false --&gt;
        &lt;property name="testOnBorrow" value="true" /&gt;
        &lt;!-- 在空闲时检查有效性, 默认false --&gt;
        &lt;property name="testWhileIdle" value="true" /&gt;
        &lt;!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --&gt;
        &lt;property name="blockWhenExhausted" value="false" /&gt;
    &lt;/bean&gt; 
    &lt;!-- jedis客户端单机版 --&gt;
    &lt;bean id="redisClient" class="redis.clients.jedis.JedisPool"&gt;
        &lt;constructor-arg name="host" value="${redis.host}" /&gt;
        &lt;constructor-arg name="port" value="${redis.ip}" /&gt;
        &lt;!-- &lt;constructor-arg name="poolConfig" ref="jedisPoolConfig" /&gt; --&gt;
    &lt;/bean&gt;
    &lt;bean id="jedisClient" class="com.itdragon.common.utils.JedisClientSingle"/&gt;
&lt;/beans&gt;</span></span></span></span></pre>
</div>
<p>简单封装了Jedis 常用方法 JedisClientSingle.java</p>
</div>
<div class="cnblogs_code">
<pre><span>package com.xyg.utils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

// 单例的Redis 工具类
public class<span> JedisClientSingle {
    
    /**
     * connect timed out 问题： 
     * 1. 检查redis服务是否开启
     * 2. 检查是否是因为防火墙的问题
     * 3. 检查网络问题(如果在同一个局域网内几乎不会出现这个问题)
     * Jedis jedis =new Jedis(HOST,PORT,100000);
     * JedisPool pool = new JedisPool(poolConfig, HOST, PORT, 100000);
     */<span>
    
    @Autowired
    private<span> JedisPool jedisPool; 
    
    public String get<span>(String key) {
        Jedis jedis =<span> jedisPool.getResource();
        String string = jedis.get<span>(key);
        jedis.close();
        return string<span>;
    }

    public String set<span>(String key, String value) {
        Jedis jedis =<span> jedisPool.getResource();
        String string = jedis.set<span>(key, value);
        jedis.close();
        return string<span>;
    }

    public<span> String hget(String hkey, String key) {
        Jedis jedis =<span> jedisPool.getResource();
        String string =<span> jedis.hget(hkey, key);
        jedis.close();
        return string<span>;
    }

    public long<span> hset(String hkey, String key, String value) {
        Jedis jedis =<span> jedisPool.getResource();
        Long result =<span> jedis.hset(hkey, key, value);
        jedis.close();
        return<span> result;
    }

    public long<span> del(String key) {
        Jedis jedis =<span> jedisPool.getResource();
        Long result =<span> jedis.del(key);
        jedis.close();
        return<span> result;
    }

    public long<span> hdel(String hkey, String key) {
        Jedis jedis =<span> jedisPool.getResource();
        Long result =<span> jedis.hdel(hkey, key);
        jedis.close();
        return<span> result;
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<h3>集群版</h3>
<div class="cnblogs_code">
<pre>/**
 * 集群版测试
 * 若提示以下类似的错误：
 * java.lang.NumberFormatException: For input string: "6002@16002"
 * 若安装的redis 版本大于4，则可能是jedis 的版本低了。选择 2.9.0
 * 因为 cluster nodes 打印的信息中，4版本之前的是没有 @16002 tcp端口信息
 * 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71:6002@16002 master - 0 1512035804722 3 connected 10923-16383
 */<span>
@Test
public void<span> testJedisCluster() throws IOException {
    HashSet&lt;HostAndPort&gt; nodes = new HashSet&lt;&gt;<span>();
    nodes.add(new HostAndPort(HOST, 7000<span>));
    nodes.add(new HostAndPort(HOST, 7001<span>));
    nodes.add(new HostAndPort(HOST, 7002<span>));
    nodes.add(new HostAndPort(HOST, 7003<span>));
    nodes.add(new HostAndPort(HOST, 7004<span>));
    nodes.add(new HostAndPort(HOST, 7005<span>));
    JedisCluster cluster = new<span> JedisCluster(nodes);
    cluster.set("cluster-key", "cluster-value"<span>);
    System.out.println("集群测试 ： " + cluster.get("cluster-key"<span>));
    cluster.close();
}</span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div></div>
</body>
</html>
